﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbstraX.Contracts;
using System.ServiceModel.DomainServices.Client;
using Mvvm;
using Microsoft.Practices.Unity;
using AbstraX;
using System.Collections.ObjectModel;
#if SILVERLIGHT
using AbstraX.ClientInterfaces;
#else
using AbstraX.ServerInterfaces;
#endif
using UIObjects.TreeViewInterfaces;
using MvvmTreeView;
using System.Windows;
using AbstraX.BuildWizard;

namespace AbstraX.ViewModels.ModelTree
{
    public class ModelViewModel : BaseViewModel<IProviderClientService>, IModelViewModel
    {
        private ModelTreeView treeView;
        private WizardContainer wizardContainer;
        public event LoadWizardWindowHandler OnLoadWizardWindow;
        public event AbstraX.Contracts.NodeSelectedHandler NodeSelected;
        public event OnDebugInfoHandler OnDebugInfo;

        public ModelViewModel()
        {
            IUnityContainer container = ProviderClientManager.Container;
            IProviderClientService model = container.Resolve<IProviderClientService>();
            IRootsCollection roots;

            this.Model = model;

            wizardContainer = new WizardContainer(this);
            this.Model.WizardContainer = wizardContainer;

            roots = this.Model.ProviderRoots;

            treeView = new ModelTreeView(model, roots);

            treeView.NodeSelected += (t, n) =>
            {
                if (NodeSelected != null)
                {
                    NodeSelected(n, ((ModelTreeNodeBase)n).BaseObject);
                }
            };

            treeView.ImageList = new ImageList();
        }

        public ITreeView TreeView
        {
            get 
            {
                return treeView;
            }
        }

        public void LoadWizardWindow(IWizardContainer wizardContainer)
        {
            IWizardContainerWindow window;

            wizardContainer.OnDebugInfo += (sender, debugInfo) =>
            {
                OnDebugInfo(sender, debugInfo);
            };

            OnLoadWizardWindow(out window, wizardContainer);

            wizardContainer.ContainerWindow = window;
        }
    }
}
